//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#if _MSC_VER > 1000
#pragma once
#endif

#ifndef __GHOST_ENUMERATOR_H__
#define __GHOST_ENUMERATOR_H__

#include <elasys_server.h>
#include <interlocked.h>
#include "ghost.h"

class CGhostEnumerator: public Ghost
{
public:
    UInt32 AddRef();
    UInt32 Release();

public:
    ECode Current(
        /* [out] */ CGhostProcess ** ppGhost);

    ECode MoveNext(
        /* [out] */ Boolean * pSuccess);

    ECode Reset();

    void GetId(
        /* [out] */ Int32 * pEnumId) {*pEnumId = m_enumeratorId; }

public:
    CGhostEnumerator(
        /* [in] */ DLinkNode * pListHead,
        /* [in] */ Int32 * pListState,
        /* [in] */ PVoid pListLock);
    Boolean IsEqual(Int32 enumId) { return (m_enumeratorId == enumId);}

public:
    STATIC ECode S_Initialize();
    STATIC void  S_Destroy();

private:
    Int32 m_enumeratorId;

    DLinkNode * m_pListHead;
    Int32     * m_pListState;
    PVoid       m_hListLock;

    DLinkNode * m_pCurrentNode;
    Int32       m_originalState;

private:
    Interlocked m_nRefs;
};

class CGhostEnumeratorFactory: public GhostFactory
{
public:
    ECode CreateGhost(
            /* [out] */ Ghost ** ppGhost,
            /* [out] */ Boolean * pbExists);

    ECode GetGhost(
            /* [out] */ Ghost ** ppGhost);

    ECode DeleteGhost(
            /* [in] */ Ghost * pGhost);

public:
    CGhostEnumeratorFactory(DLinkNode * pListNode,
                            Int32 * pListState,
                            PVoid  pListLock)
                          : m_pListHead(pListNode),
                            m_pListState(pListState),
                            m_hListLock(pListLock) {}

    CGhostEnumeratorFactory(Int32 m_enumId)
                          : m_enumeratorId(m_enumId) {}

private:
    Int32 m_enumeratorId;

    DLinkNode * m_pListHead;
    Int32     * m_pListState;
    PVoid       m_hListLock;
};

#endif // __GHOST_ENUMERATOR_H__

